from faker import Faker
import random
from openpyxl import Workbook
from db_utils import *

fake = Faker('zh_CN')


def generate_random_student():
    name = fake.name()
    gender = random.choice(["男", "女"])
    age = random.randint(18, 25)
    chinese_score = round(random.uniform(60, 100), 2)
    math_score = round(random.uniform(60, 100), 2)
    english_score = round(random.uniform(60, 100), 2)
    student_id = f"S{random.randint(10000, 99999)}"
    classes = ['6年级1班', '6年级2班', '6年级3班', '6年级4班', '6年级5班', '6年级6班']
    class_name = random.choice(classes)
    return (name, gender, age, student_id, class_name, chinese_score, math_score, english_score)


def bulk_add_students():
    try:
        count = int(input("请输入要添加的学生数量: "))
        for _ in range(count):
            student = generate_random_student()
            insert_student(student)
        print(f"已成功插入 {count} 条学生记录")
    except Exception as e:
        print("插入失败: ", e)


def export_to_excel():
    students = get_all_students()
    wb = Workbook()
    ws = wb.active
    ws.title = "学生成绩"

    headers = ["ID", "姓名", "性别", "年龄", "学号", "班级", "语文成绩", "数学成绩", "英语成绩"]
    ws.append(headers)

    for s in students:
        ws.append([
            s['id'],
            s['name'],
            s['gender'],
            s['age'],
            s['student_id'],
            s['class'],
            s['chinese_score'],
            s['math_score'],
            s['english_score']
        ])
    filename = "students_scores.xlsx"
    wb.save(filename)
    print(f"学生成绩已导出到 {filename}")


def login_for_delete():
    username = input("请输入管理员用户名: ")
    password = input("请输入密码: ")

    if username == "admin" and password == "123456":
        print("认证通过")
        return True
    else:
        print("认证失败,无权限删除")
        return False